package com.yc.cloud.openai.portal.controller;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.openai.portal.dto.HistoryDto;
import com.yc.cloud.openai.portal.dto.PromptDto;
import com.yc.cloud.openai.portal.dto.QueueDto;
import com.yc.cloud.openai.portal.dto.UploadImageDto;
import com.yc.cloud.openai.portal.dto.request.TextToImageHistoryRequest;
import com.yc.cloud.openai.portal.dto.request.TextToImageInterruptRequest;
import com.yc.cloud.openai.portal.dto.request.TextToImageQueueCancelRequest;
import com.yc.cloud.openai.portal.dto.request.TextToImageQueueRequest;
import com.yc.cloud.openai.portal.dto.request.TextToImageRequest;
import com.yc.cloud.openai.portal.dto.request.TextToImageUploadRequest;
import com.yc.cloud.openai.portal.dto.response.ComfyUiQueueResponse;
import com.yc.cloud.openai.portal.service.TextToImageService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
/**
 * AIGC/图片模块
 */
@RestController
@RequestMapping("image")
@Api(value = "AI绘图接口", tags = "AI绘图接口")
public class ImageController {

    private final TextToImageService textToImageService;

    public ImageController(TextToImageService textToImageService) {
        this.textToImageService = textToImageService;
    }

    @PostMapping("/text-to-image")
    @ApiOperation(value = "文本转图片", notes = "将文本转化为语音,返回任务ID,后续通过该任务ID可以查询生成的图片、任务的排队信息等")
    public ResponseResult<PromptDto> convertTextToImage(@RequestBody TextToImageRequest request) {
        val response = textToImageService.convertTextToImage(request);
        return ResponseResult.success(response);
    }

    @GetMapping("/queue")
    @ApiOperation(value = "通过任务ID查询排队信息", notes = "通过任务ID获取文生图的排队信息")
    public ResponseResult<QueueDto> queue(@Validated TextToImageQueueRequest request) {
        val response = textToImageService.queue(request);
        return ResponseResult.success(response);
    }

    @GetMapping("/history")
    @ApiOperation(value = "通过任务ID获取生成的图片列表", notes = "通过任务ID获取生成图片列表")
    public ResponseResult<HistoryDto> history(@Validated TextToImageHistoryRequest request) {
        val response = textToImageService.history(request);
        return ResponseResult.success(response);
    }

    @PostMapping("/queue")
    @ApiOperation(value = "通过任务ID取消排队中的任务", notes = "通过任务ID取消任务,如果没有传任务ID,则取消所有排队中的任务")
    public ResponseResult<ComfyUiQueueResponse> cancelQueue(@Validated TextToImageQueueCancelRequest request) {
        val response = textToImageService.cancelQueue(request);
        return ResponseResult.success(response);
    }

    @PostMapping("/interrupt")
    @ApiOperation(value = "通过任务ID取消正在运行的任务", notes = "通过任务ID取消任务,如果没有传任务ID,则取消所有排队中的任务")
    public ResponseResult<Boolean> interrupt(@Validated @RequestBody TextToImageInterruptRequest request) {
        textToImageService.interrupt(request);
        return ResponseResult.success(true);
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传图片", notes = "上传图片")
    public ResponseResult<UploadImageDto> upload(TextToImageUploadRequest request,MultipartFile file) {
        val response = textToImageService.upload(request,file);
        return ResponseResult.success(response);
    }

}
